package cn.edu.dlut.ssdut.foodCredit2012.action;


//import java.util.Calendar;

//import cn.edu.dlut.ssdut.foodCredit2012.model.User;
import java.util.Date;

import cn.edu.dlut.ssdut.foodCredit2012.dto.UserLoginDTO;
import cn.edu.dlut.ssdut.foodCredit2012.dto.UserRegisterDTO;
import cn.edu.dlut.ssdut.foodCredit2012.model.Basic;
import cn.edu.dlut.ssdut.foodCredit2012.model.BasicCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.Customs;
import cn.edu.dlut.ssdut.foodCredit2012.model.CustomsCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.Export;
import cn.edu.dlut.ssdut.foodCredit2012.model.ExportCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.Finance;
import cn.edu.dlut.ssdut.foodCredit2012.model.FinanceCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.Permission;
import cn.edu.dlut.ssdut.foodCredit2012.model.PermissionCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.Presentation;
import cn.edu.dlut.ssdut.foodCredit2012.model.PresentationCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.Quality;
import cn.edu.dlut.ssdut.foodCredit2012.model.QualityCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.Tax;
import cn.edu.dlut.ssdut.foodCredit2012.model.TaxCollection;
import cn.edu.dlut.ssdut.foodCredit2012.model.User;
import cn.edu.dlut.ssdut.foodCredit2012.model.UserCollection;
import cn.edu.dlut.ssdut.foodCredit2012.service.BasicCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.BasicService;
import cn.edu.dlut.ssdut.foodCredit2012.service.CustomsCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.CustomsService;
import cn.edu.dlut.ssdut.foodCredit2012.service.ExportCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.ExportService;
import cn.edu.dlut.ssdut.foodCredit2012.service.FinanceCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.FinanceService;
import cn.edu.dlut.ssdut.foodCredit2012.service.PermissionCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.PermissionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.PresentationCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.PresentationService;
import cn.edu.dlut.ssdut.foodCredit2012.service.QualityCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.QualityService;
import cn.edu.dlut.ssdut.foodCredit2012.service.TaxCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.TaxService;
import cn.edu.dlut.ssdut.foodCredit2012.service.UserCollectionService;
import cn.edu.dlut.ssdut.foodCredit2012.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private String name = "shaonvshidai";
	
	private String licenceID;
	
	private User user = new User();
	private Basic basic = new Basic();
	private Customs customs = new Customs();
	private Export export = new Export();
	private Finance finance = new Finance();
	private Permission permission = new Permission();
	private Presentation presentation = new Presentation();
	private Tax tax = new Tax();
	private Quality quality = new Quality();
	
	private UserCollection userC = new UserCollection();
	private BasicCollection basicC = new BasicCollection();
	private CustomsCollection customsC = new CustomsCollection();
	private ExportCollection exportC = new ExportCollection();
	private PermissionCollection permissionC = new PermissionCollection();
	private PresentationCollection presentationC = new PresentationCollection();
	private TaxCollection taxC = new TaxCollection();
	private QualityCollection qualityC = new QualityCollection();
	private FinanceCollection financeC = new FinanceCollection();
	
	
	private UserService userS = new UserService();
	private BasicService basicS = new BasicService();
	private CustomsService customsS = new CustomsService();
	private ExportService exportS = new ExportService();
	private FinanceService financeS = new FinanceService();
	private PermissionService permissionS = new PermissionService();
	private PresentationService presentationS = new PresentationService();
	private TaxService taxS = new TaxService();
	private QualityService qualityS = new QualityService();
	
	
	
	private UserCollectionService userCS = new UserCollectionService();
	private BasicCollectionService basicCS = new BasicCollectionService();
	private CustomsCollectionService customsCS = new CustomsCollectionService();
	private ExportCollectionService exportCS = new ExportCollectionService();
	private FinanceCollectionService financeCS = new FinanceCollectionService();
	private PermissionCollectionService permissionCS = new PermissionCollectionService(); 
	private PresentationCollectionService presentationCS = new PresentationCollectionService();
	private TaxCollectionService taxCS = new TaxCollectionService();
	private QualityCollectionService qualityCS = new QualityCollectionService();
	
	private UserLoginDTO userlogin = new UserLoginDTO();
	private UserRegisterDTO userRegister = new UserRegisterDTO();
	
		
	
	public String login() {
		return SUCCESS;
	}
	
	public String loginInput() throws Exception{
		this.name = this.userlogin.getName();
		
		this.user.setLicenceID(this.userlogin.getLicenceID());
		this.user.setName(this.userlogin.getName());
		this.user.setPassWord(this.userlogin.getPassword());
		
		if(userS.login(user)){
			this.user = this.userS.loadByLicenceID(this.user.getLicenceID());
			
			user.setLastLoginTime(user.getLoginTime());
			user.setLoginTime(new Date());
			user.setCount(user.getCount()+1);
			
			this.userS.update(user);
			
			if(user.getInfo())  return "login_success";
			return "user_infoForm";
		}
		return "login_fail";
		
		
	}
	
	
	public String register()throws Exception {
		return SUCCESS;
	}
	
	public String registerInput() throws Exception{
		this.user.setLicenceID(this.userRegister.getLicenceID());
		this.user.setName(this.userRegister.getName());
		this.user.setPassWord(this.userRegister.getPassword());
		this.user.setRegisterTime(new Date());
		
		if(!this.userS.isUser(user)){
			if(this.userS.isPotentialUser(user)){
				System.out.println("user is a potentialUser"+ user.getLicenceID());
				System.out.println("user is a potentialUser" + user.getName());
				this.userS.add(user);
				return "register_success";
			}
			this.userC.setLicenceID(this.userRegister.getLicenceID());
			this.userC.setName(this.userRegister.getName());
			this.userC.setPassWord(this.userRegister.getPassword());
			this.userC.setRegisterTime(new Date());
			
			if(this.userCS.isUserCollection(userC)) return "register_wait_to_check";
			this.userCS.add(userC);
			return "register_wait_to_check";
		}
		return "register_fail";	
	}
	
	public String infoForm(){
		return SUCCESS;
	}
	
	public String infoInput() throws Exception {
		System.out.println(this.basicC.getLicenceID());
		this.customsC.setLicenceID(basicC.getLicenceID());
		this.exportC.setLicenceID(this.basicC.getLicenceID());
		this.financeC.setLicenceID(this.basicC.getLicenceID());
		this.permissionC.setLicenceID(this.basicC.getLicenceID());
		this.presentationC.setLicenceID(this.basicC.getLicenceID());
		this.taxC.setLicenceID(this.basicC.getLicenceID());
		this.qualityC.setLicenceID(this.basicC.getLicenceID());
		
		this.basicCS.add(basicC);
		this.customsCS.add(customsC);
		this.exportCS.add(exportC);
		this.financeCS.add(financeC);
		this.permissionCS.add(permissionC);
		this.presentationCS.add(presentationC);
		this.taxCS.add(taxC);
		this.qualityCS.add(qualityC);
		
		System.out.println(this.basicCS.hasBasic(basicC));
		
		return SUCCESS;
	}

	public String load()throws Exception{
		
		if(user.getInfo()){
			this.basic = this.basicS.loadByLicenceID(user.getLicenceID());
			this.customs = this.customsS.loadByLicenceID(user.getLicenceID());
			this.export = this.exportS.loadByLicenceID(user.getLicenceID());
			this.finance = this.financeS.loadByLicenceID(user.getLicenceID());
			this.permission = this.permissionS.loadByLicenceID(user.getLicenceID());
			this.presentation = this.presentationS.loadByLicenceID(user.getLicenceID());
			this.tax = this.taxS.loadByLicenceID(user.getLicenceID());
			this.quality = this.qualityS.loadByLicenceID(user.getLicenceID());
		}
		
	
		return SUCCESS;
	}
	
	public String customs()  throws Exception{
		this.customs = this.customsS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String customsUpdate()throws Exception {
		this.customs = this.customsS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String customsUpdateInput() throws Exception{
		this.customsS.update(customs);
		return "updateInput";
	}
	public String export() throws Exception {
		this.export = this.exportS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	
	public String exportUpdate()throws Exception{
		this.export = this.exportS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	
	public String exportUpdateInput() throws Exception{
		this.exportS.update(export);
		return "updateInput";
	}
	
	public String finance() throws Exception {
		this.finance = this.financeS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String financeUpdate() throws Exception{
		this.finance = this.financeS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String financeUpdateInput()throws Exception {
		this.financeS.update(finance);
		return "updateInput";
	}
	
	public String permission() throws Exception {
		this.permission = this.permissionS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String permissionUpdate() throws Exception{
		this.permission = this.permissionS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String permissionUpdateInput()throws Exception {
		this.permissionS.update(permission);
		return "updateInput";
	}
	
	public String presentation()  throws Exception{
		this.presentation = this.presentationS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	
	public String presentationUpdate() throws Exception{
		this.presentation = this.presentationS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	
	public String presentationUpdateInput() throws Exception {
		System.out.println("**********************************"+this.presentation.getLicenceID());
		this.presentationS.update(presentation);
		return "updateInput";
	}
	
	public String quality() throws Exception{
		this.quality = this.qualityS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	
	public String qualityUpdate() throws Exception{
		this.quality = this.qualityS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String qualityUpdateInput()throws Exception {
		System.out.println("quality}}}}}}}}}}}}}}}}}}"+this.quality.getName());
		this.qualityS.update(quality);
		return "updateInput";
	}
	
	public String tax() throws Exception {
		this.tax = this.taxS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	
	public String taxUpdate() throws Exception{
		this.tax = this.taxS.loadByLicenceID(licenceID);
		return SUCCESS;
		
	}
	public String taxUpdateInput() throws Exception {
		this.taxS.update(tax);
		return "updateInput";
	}
	
	public String basic() {
		this.basic = this.basicS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String basicUpdate(){
		this.basic = this.basicS.loadByLicenceID(licenceID);
		return SUCCESS;
	}
	public String basicUpdateInput()throws Exception {
		this.basicS.update(basic);
		return "updateInput";
	}
	

	public UserService getUserS() {
		return userS;
	}

	public void setUserS(UserService userS) {
		this.userS = userS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserCollectionService getUserCS() {
		return userCS;
	}

	public void setUserCS(UserCollectionService userCS) {
		this.userCS = userCS;
	}


	public UserLoginDTO getUserlogin() {
		return userlogin;
	}

	public void setUserlogin(UserLoginDTO userlogin) {
		//System.out.println("in userloginDTO SET "+ userlogin.getLicenceID());
		this.userlogin = userlogin;
		//System.out.println("in userloginDTO SET "+ userlogin.getLicenceID());
	}

	public Basic getBasic() {
		//System.out.println("getBasic licenceID}}}}}}}}}}}}}}" + this.basic.getLicenceID());
		return basic;
	}

	public void setBasic(Basic basic) {
		
		this.basic = basic;
	}

	public Customs getCustoms() {
		return customs;
	}

	public void setCustoms(Customs customs) {
		this.customs = customs;
	}

	public Export getExport() {
		return export;
	}

	public void setExport(Export export) {
		this.export = export;
	}

	public Finance getFinance() {
		return finance;
	}

	public void setFinance(Finance finance) {
		this.finance = finance;
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	public Presentation getPresentation() {
		return presentation;
	}

	public void setPresentation(Presentation presentation) {
		this.presentation = presentation;
	}

	public Tax getTax() {
		return tax;
	}

	public void setTax(Tax tax) {
		this.tax = tax;
	}

	public Quality getQuality() {
		return quality;
	}

	public void setQuality(Quality quality) {
		this.quality = quality;
	}

	public BasicService getBasicS() {
		return basicS;
	}

	public void setBasicS(BasicService basicS) {
		this.basicS = basicS;
	}

	public CustomsService getCustomsS() {
		return customsS;
	}

	public void setCustomsS(CustomsService customsS) {
		this.customsS = customsS;
	}

	public ExportService getExportS() {
		return exportS;
	}

	public void setExportS(ExportService exportS) {
		this.exportS = exportS;
	}

	public FinanceService getFinanceS() {
		return financeS;
	}

	public void setFinanceS(FinanceService financeS) {
		this.financeS = financeS;
	}

	public PermissionService getPermissionS() {
		return permissionS;
	}

	public void setPermissionS(PermissionService permissionS) {
		this.permissionS = permissionS;
	}

	public PresentationService getPresentationS() {
		return presentationS;
	}

	public void setPresentationS(PresentationService presentationS) {
		this.presentationS = presentationS;
	}

	public TaxService getTaxS() {
		return taxS;
	}

	public void setTaxS(TaxService taxS) {
		this.taxS = taxS;
	}

	public QualityService getQualityS() {
		return qualityS;
	}

	public void setQualityS(QualityService qualityS) {
		this.qualityS = qualityS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLicenceID() {
		return licenceID;
	}

	public void setLicenceID(String licenceID) {
		this.licenceID = licenceID;
	}

	public UserRegisterDTO getUserRegister() {
		return userRegister;
	}

	public void setUserRegister(UserRegisterDTO userRegister) {
		this.userRegister = userRegister;
	}

	public UserCollection getUserC() {
		return userC;
	}

	public void setUserC(UserCollection userC) {
		this.userC = userC;
	}

	public BasicCollection getBasicC() {
		return basicC;
	}

	public void setBasicC(BasicCollection basicC) {
		this.basicC = basicC;
	}

	public CustomsCollection getCustomsC() {
		return customsC;
	}

	public void setCustomsC(CustomsCollection customsC) {
		this.customsC = customsC;
	}

	public ExportCollection getExportC() {
		return exportC;
	}

	public void setExportC(ExportCollection exportC) {
		this.exportC = exportC;
	}

	public PermissionCollection getPermissionC() {
		return permissionC;
	}

	public void setPermissionC(PermissionCollection permissionC) {
		this.permissionC = permissionC;
	}

	public PresentationCollection getPresentationC() {
		return presentationC;
	}

	public void setPresentationC(PresentationCollection presentationC) {
		this.presentationC = presentationC;
	}

	public TaxCollection getTaxC() {
		return taxC;
	}

	public void setTaxC(TaxCollection taxC) {
		this.taxC = taxC;
	}

	public QualityCollection getQualityC() {
		return qualityC;
	}

	public void setQualityC(QualityCollection qualityC) {
		this.qualityC = qualityC;
	}

	public FinanceCollection getFinanceC() {
		return financeC;
	}

	public void setFinanceC(FinanceCollection financeC) {
		this.financeC = financeC;
	}

	public BasicCollectionService getBasicCS() {
		return basicCS;
	}

	public void setBasicCS(BasicCollectionService basicCS) {
		this.basicCS = basicCS;
	}

	public CustomsCollectionService getCustomsCS() {
		return customsCS;
	}

	public void setCustomsCS(CustomsCollectionService customsCS) {
		this.customsCS = customsCS;
	}

	public ExportCollectionService getExportCS() {
		return exportCS;
	}

	public void setExportCS(ExportCollectionService exportCS) {
		this.exportCS = exportCS;
	}

	public FinanceCollectionService getFinanceCS() {
		return financeCS;
	}

	public void setFinanceCS(FinanceCollectionService financeCS) {
		this.financeCS = financeCS;
	}

	public PermissionCollectionService getPermissionCS() {
		return permissionCS;
	}

	public void setPermissionCS(PermissionCollectionService permissionCS) {
		this.permissionCS = permissionCS;
	}

	public PresentationCollectionService getPresentationCS() {
		return presentationCS;
	}

	public void setPresentationCS(PresentationCollectionService presentationCS) {
		this.presentationCS = presentationCS;
	}

	public TaxCollectionService getTaxCS() {
		return taxCS;
	}

	public void setTaxCS(TaxCollectionService taxCS) {
		this.taxCS = taxCS;
	}

	public QualityCollectionService getQualityCS() {
		return qualityCS;
	}

	public void setQualityCS(QualityCollectionService qualityCS) {
		this.qualityCS = qualityCS;
	}



	
}
